-- 烬策技能
-- 出牌阶段，你可令一名角色将一张牌当【铁索连环】使用或重铸，然后若其手牌的花色数为4，其视为使用【火攻】。

local DIY = require "packages/diy_utility/diy_utility"
local ls = require "packages/lvshi/utility"
-- 获取中央区类别数的辅助函数
local function getCenterTypeCount(room)
  local center_cards = DIY.getCenterCards(room)
  local types = {}
  for _, id in ipairs(center_cards) do
    local card = Fk:getCardById(id)
    if card then
      types[card.type] = true
    end
  end
  return #types
end

local jinze = fk.CreateSkill{
  name = "lvshi__jinze",
}

jinze:addEffect("active", {
  anim_type = "control",
  prompt = "#jinze-invoke",
  card_num = 0,
  target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and not to_select:isNude()
  end,
  can_use = function(self, player)
    local max_times = getCenterTypeCount(Fk:currentRoom()) + 1
    local use_times = player:usedSkillTimes("lvshi__jinze") + player:usedSkillTimes("lvshi__chengmou")
    if player:hasSkill("lvshi__cunmu", true) then
      return use_times < max_times
    else
      return true
    end
  end,
  on_use = function(self, room, effect)
    local target = effect.tos[1]
    -- 先令目标使用铁索连环
    local useData = room:askToUseVirtualCard(target, {
      name = "lvshi__iron_chain",
      skill_name = jinze.name,
      prompt = "#jinze_use_tiesuo",
      cancelable = true,
      card_filter = {
        n = 1,
        cards = target:getCardIds("he"),
      },
    })
    if not useData then
      if not target:isNude() then
        local card_id = room:askToCards(target, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = jinze.name,
          cancelable = false,
          prompt = "#jinze_recast_card"
        })
        if card_id then
          room:recastCard(card_id, target, jinze.name)
        end
      end
    end
    
    -- 如果目标手牌花色数为4，则视为使用火攻
    if ls.countSuits(target:getCardIds("h")) >= 4 then
      room:askToUseVirtualCard(target, {
        name = "lvshi__fire_attack",
        skill_name = jinze.name,
        prompt = "#jinze_use_fire",
        cancelable = true,
      })
    end
  end,
})

Fk:loadTranslationTable{
  ["lvshi__jinze"] = "烬策",
  [":lvshi__jinze"] = "出牌阶段，你可以令一名角色将一张牌当【铁索连环】使用或重铸，然后若其手牌花色数为4，其视为使用【火攻】。",
  
  ["#jinze-invoke"] = "烬策：是否令一名角色将一张牌当【铁索连环】使用或重铸？",
  ["#jinze_use_tiesuo"] = "烬策：你可以将一张牌当【铁索连环】使用",
  ["#jinze_recast_card"] = "烬策：请选择一张手牌重铸",
  ["#jinze_use_fire"] = "烬策：你可以视为使用【火攻】",
}

return jinze